/*
 * system.h
 *
 *  Created on: Apr 2, 2012
 *      Author: Tri_D
 */

#ifndef SYSTEM_H_
#define SYSTEM_H_

#include "main header.h"
#include "serial.h"
#include "display.h"
#include "color.h"
#include "key.h"


#define	MAX_SERIAL_TIME_OUT		8					// 4 count = 1sec	

typedef enum Battery
{
	NO_BAT = 0,
	EMPTY_BAT,
	LV1_BAT,
	LV2_BAT,
	LV3_BAT,
	LV4_BAT
} Bat_t;


typedef struct _Time
{
	volatile uint8_t hour;
	volatile uint8_t min;
	volatile uint8_t sec;
	volatile uint8_t q_sec;
	int8_t	bOn;
} Time_t;


typedef struct _Sys
{
	Serial_t Ser;
	uint16_t Annuc;			// 8 down arrows, Cal, zero, stable, L_Colon, R_Colon
	uint8_t Str[9];			// 6 digit and might be some decimal and colons
	uint8_t Unit[3];		// Unit
	uint8_t Mode;			// Mode
	Bat_t Bat;				// battery level
	uint8_t Color[3];		// Set background color
	Time_t Time;			// Time module
	KeyBrd_t keybrd;		// keyboard module
	BOOL bRdy;				// Ready for display
	BOOL bUpdate;			// Flag indicate needs to update display
} Sys_t;

extern Sys_t Sys; 

void Sys_Init(Sys_t * pS);
void Sys_Reset(Sys_t * pS);
void Timer_Init(Time_t * pT);
void delay_1sec(void);
void delay_500ms(void);
void delay_250ms(void);
void delay_sec(uint8_t sec);
uint8_t Read_Timer_Q_Sec(Time_t * pT);

uint8_t Ascii2Val(uint8_t *psrc, uint8_t size);
void Val2Ascii(uint8_t val, uint8_t *pdes);

void Process_Serial_Data(Serial_t * pS, Time_t * pT);
void process_keybrd_intent(void);

#endif /* SYSTEM_H_ */
